package cn.iocoder.yudao.module.promotion.controller.admin.prizedraw;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.*;
import cn.iocoder.yudao.module.promotion.service.prizedraw.PrizeDrawSettingBatchService;
import cn.iocoder.yudao.module.promotion.service.prizedraw.PrizeDrawSettingProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 抽奖设置商品批次")
@RestController
@RequestMapping("/promotion/prize-draw-setting-batch")
@Validated
public class PrizeDrawSettingBatchController {

    @Resource
    private PrizeDrawSettingBatchService prizeDrawSettingBatchService;

    @Resource
    private PrizeDrawSettingProductService iPrizeDrawSettingProductService;

    @GetMapping("/page")
    @Operation(summary = "抽奖设置商品批次分页列表")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-setting-batch:query')")
    public CommonResult<PageResult<PrizeDrawSettingBatchRespVO>> paging(PrizeDrawSettingBatchPageReqVO dto) {
        return success(prizeDrawSettingBatchService.paging(dto));
    }

    @Operation(summary = "抽奖设置商品批次详情信息")
    @GetMapping("/getInfoById")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-setting-batch:query')")
    public CommonResult<PrizeDrawSettingBatchRespVO> getInfoById(@NotNull(message = "抽奖设置商品批次Id不能为空") @RequestParam(value = "prizeDrawId") Long prizeDrawId) {
        return success(prizeDrawSettingBatchService.getInfoById(prizeDrawId));
    }


    @Operation(summary = "抽奖设置商品批次-商品列表")
    @GetMapping("/getProductByBatchId")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-setting-batch:query')")
    public CommonResult<List<PrizeDrawSettingProductRespVO>> getProductByBatchId(@NotNull(message = "抽奖设置商品批次Id不能为空")
                                                                                 @RequestParam(value = "prizeDrawSettingBatchId") Long prizeDrawSettingBatchId) {
        List<PrizeDrawSettingProductRespVO> productsByBatchId = iPrizeDrawSettingProductService.getProductsByBatchId(prizeDrawSettingBatchId);
        if (CollUtil.isNotEmpty(productsByBatchId)) {
            List<Long> syIds = prizeDrawSettingBatchService.shuyinProducts(ListUtil.list(false, prizeDrawSettingBatchId));
            List<Long> fsyIds = CollUtil.isEmpty(syIds) ? ListUtil.list(false, -1L) : syIds;
            productsByBatchId.removeIf(product -> !fsyIds.contains(product.getSkuId()));
        }

        return success(productsByBatchId);
    }

    @Operation(summary = "新增抽奖设置商品批次")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-setting-batch:create')")
    public CommonResult<Boolean> add(@Validated @RequestBody PrizeDrawSettingBatchDto setting) {
        prizeDrawSettingBatchService.preserve(setting);
        return success(true);
    }

    @Operation(summary = "修改抽奖设置商品批次")
    @PutMapping
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-setting-batch:update')")
    public CommonResult<Boolean> edit(@Validated @RequestBody PrizeDrawSettingBatchDto setting) {
        prizeDrawSettingBatchService.preserve(setting);
        return success(true);
    }

    @Operation(summary = "启停")
    @PostMapping("/updown")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-setting-batch:updown')")
    public CommonResult<Boolean> updown(@RequestBody PrizeDrawSettingBatchUpdownDto dto) {
        prizeDrawSettingBatchService.updown(dto);
        return success(true);
    }

    @Operation(summary = "删除")
    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-setting-batch:delete')")
    public CommonResult<Boolean> delete(@RequestBody PrizeDrawSettingBatchDeleteDto dto) {
        prizeDrawSettingBatchService.delete(dto);
        return success(true);
    }

}